const FileService = require('../service/file.service')
const userService = require('../service/user.service')
const { SERVER_HOST,SERVER_PROT } = require('../config/server')

class fileController {
  // 1上传图片
  async create(ctx, next) {
    // filenamme,mimetype,size,user_id
    const { id } = ctx.user
    const { filename, mimetype, size } = ctx.request.file
    // console.log(filename, mimetype, size, id)
    const result = await FileService.create(filename, mimetype, size, id)
    // 把头像保存到user表中
    const avatarUrl=`${SERVER_HOST}:${SERVER_PROT}/users/avatar/${id}`
    console.log(avatarUrl,id)
    const result1=await userService.updateUserAvatar(avatarUrl,id)
    console.log(result1)
    ctx.body = {
      code: 0,
      message: '上传成功',
      data: result,
    }
  }
  async savePictureInfo(ctx,next){
    // console.log(ctx.request.files)
    const files=ctx.request.files
    // filename,minmeype,size,moment_id,user_id
    const {id}=ctx.user
    // const {filename,minmeype,size}=ctx.request.files
    // console.log(ctx.query)
    const {momentId}=ctx.query
    for(const file of files){
      const {filename,mimetype,size}=file
      const result=await FileService.createFile(filename, mimetype, size, id, momentId);
    }
      ctx.body = '动态配图上传完成~'
  }
}

module.exports = new fileController()
